<script>
    import {shareName,sharedParam} from '../stores/userStore'
    import { onMount } from 'svelte';
    let currentname = '';
    let paramValue;

    let person = {}
    onMount(() => {
   const unsubscribe = sharedParam.subscribe(value => {
    paramValue = value;

   });

    return unsubscribe;
  });
  onMount(() => {
   const unsubscribe = shareName.subscribe(value => {
    currentname = value;

   });

    return unsubscribe;
  });
  let response='';

  sharedParam.subscribe(value =>{
    paramValue = value;
  });
  function change1(){
    sharedParam.set(!sharedParam)
  }
  let isdelete1 = false

  function isdelete2(){
    isdelete1 = true;
  }

    
    let logout=(e)=>{
   fetch('/api/person',{
    method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({ currentname }),
  })
  .then((v)=>{
    v.text().then((t)=>{
      response = t;
      if(response=="删除成功"){
        alert(response);
        change1();
        location.href="/login";
      }
    });
  })
  .catch((err)=>{
    response = err ;
  });
   }



  
  </script>


{#if Boolean(paramValue)}
  {#if isdelete1}
    <h>确定要注销吗？</h>
    <button on:click={logout}>确认</button>
    <button>取消</button>
  {/if}
  {#if !isdelete1}
<button on:click={isdelete2}>注销</button>
  {/if}



{:else}
  <p>请先登录</p>
  <a href="/login">登录</a>

{/if}

<style></style>
